/*******************************************************************************
  * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
  * @file     textLabel.c
  * @author   UartTFT Application Team
  * @version  V0.0.1
  * @date     2023-01-04
  * @brief    This file contains the functions prototypes for the UartTFT
  *           Application display firmware.
 ********************************************************************************/
#include "textLabel.h"
#include "levetop.h"
#include "data.h"
#include "keyBoard.h"
#include "popupBox.h"

uint8_t font_layflag = 0;
/***********************************************************************************
* Function Name : text_display
* Description   : Show text.
* Input         : - type: text info sources
				  - buff: text info
* Output        : None
* Return        : None
************************************************************************************/
void text_display(uint8_t type, uint8_t *buff)
{
	uint16_t xs, ys, xe, ye; // Display box
	uint8_t Zk_id, w, h;	 // Font ID
	uint8_t encode;			 // encoding type
	uint8_t Alignment, transparent, star;
	uint32_t fcolor, bcolor; // The foreground color of the font, The background color of the font
	uint16_t p_add, p_len, pointer;
	uint32_t zk_addr;
	uint8_t buff1[4] = {0};
	uint8_t lay_flag;

	uint8_t multi_flag, multi_OK;
	uint16_t multi_num;
	uint16_t len = 0;
	uint8_t strbuf[1024] = {0};

	if (type == 0) // instruction information source is configuration file
	{
		p_add = (buff[5] + (buff[6] << 8)) * 2;
		p_len = buff[7] + (buff[8] << 8);
		xs = buff[9] + (buff[10] << 8);
		ys = buff[11] + (buff[12] << 8);
		xe = buff[13] + (buff[14] << 8);
		ye = buff[15] + (buff[16] << 8);
		w = buff[17];
		h = buff[18];
		Zk_id = buff[19];
		encode = buff[20];
		Alignment = buff[21];
		bcolor = buff[22] + (buff[23] << 8) + (buff[24] << 16);
		fcolor = buff[25] + (buff[26] << 8) + (buff[27] << 16);
		transparent = buff[28] & 0x01;
		star = (buff[28] & 0x02) >> 1;

		if(UI_version >= 3100)
		{
			multi_flag = buff[31];
			multi_num = buff[32] + (buff[33] << 8);
		}
		else
		{
			multi_flag = 0;
		}
	}
	else if (type == 1) // instruction information ource is variable space
	{
		pointer = (buff[2] + (buff[3] << 8)) * 2;
		p_add = (var[pointer + 1] + (var[pointer + 2] << 8)) * 2;
		p_len = var[pointer + 3] + (var[pointer + 4] << 8);
		xs = var[pointer + 5] + (var[pointer + 6] << 8);
		ys = var[pointer + 7] + (var[pointer + 8] << 8);
		xe = var[pointer + 9] + (var[pointer + 10] << 8);
		ye = var[pointer + 11] + (var[pointer + 12] << 8);
		w = var[pointer + 13];
		h = var[pointer + 14];
		Zk_id = var[pointer + 15];
		encode = var[pointer + 16];
		Alignment = var[pointer + 17];
		bcolor = var[pointer + 18] + (var[pointer + 19] << 8) + (var[pointer + 20] << 16);
		fcolor = var[pointer + 21] + (var[pointer + 22] << 8) + (var[pointer + 23] << 16);
		transparent = var[pointer + 24] & 0x01 ;
		star = (var[pointer + 24] & 0x02) >> 1;

	}

	if (Pop_up_flag == 1) // Popup offset
	{
		xs += Pop_up_x;
		ys += Pop_up_y;
		xe += Pop_up_x;
		ye += Pop_up_y;
	}

	if (multi_flag == 1)
	{
		LT_ReadFlash_UI(buff1, addr_index[24] + multi_language *  addr_index[25] * 4 + (multi_num - 1) * 4, 4);
		zk_addr = buff1[0] + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);

		if (zk_addr != 0xFFFFFFFF)
		{
			multi_OK = 1;
			LT_ReadFlash_UI(buff1, zk_addr, 6);
			fcolor = buff1[0] + (buff1[1] << 8) + (buff1[2] << 16);
			Zk_id = buff1[3];
			len	= buff1[4] + (buff1[5] << 8);
			LT_ReadFlash_UI(strbuf, zk_addr + 6, len);
		}
		else
			multi_OK = 0;
	}

	LT_ReadFlash_UI(buff1, addr_index[2] + Zk_id * 8, 4);
	zk_addr = buff1[0] + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);

	if (multi_flag == 1 && multi_OK == 1)
	{
		LT_ReadFlash_UI(buff1, zk_addr , 4);
		w = buff1[2];
		h = buff1[3];

		if (buff1[0] == 0x47 && buff1[1] == 0x42)	//GB2312
			encode = 0;
		else if (buff1[0] == 0x47 && buff1[1] == 0x4B)	//GBK
			encode = 1;
		else if (buff1[0] == 0x42 && buff1[1] == 0x47)	//BIG5
			encode = 2;
		else if (buff1[0] == 0x41 && buff1[1] == 0x53)	//ASCII
			encode = 4;
		else if (buff1[0] == 0x75 && buff1[1] == 0x6e)	//UINICODE
			encode = 6;
		else if (buff1[0] == 0x53 && buff1[1] == 0x53)	//str
			encode = 9;
	}

	if (pic_update == 0)
	{
		if (multi_flag == 1 && multi_OK == 1)
		{
			LT_Print_zk_Font(encode, zk_addr,
							 font_layflag, LCD_XSIZE_TFT, w, h, Alignment, fcolor, bcolor, transparent, PIC_ON_BaseImg, PICINFO[0], star,
							 xs, ys, xe, ye,
							 0, 0, strbuf, len);
		}
		else
		{

			LT_Print_zk_Font(encode, zk_addr,
							 font_layflag, LCD_XSIZE_TFT, w, h, Alignment, fcolor, bcolor, transparent, PIC_ON_BaseImg, PICINFO[0], star,
							 xs, ys, xe, ye,
							 0, 0, &var[p_add], p_len * 2);
		}
	}
	else
	{
		if(download_flag == 3)
		{
			if (Screen_DIR == 0 || Screen_DIR == 2)
			{
				xs += LCD_XSIZE_TFT * 2;
				xe += LCD_XSIZE_TFT * 2;
			}
			else if (Screen_DIR == 1 || Screen_DIR == 3)
			{
				ys += LCD_YSIZE_TFT * 2;
				ye += LCD_YSIZE_TFT * 2;
			}
		}

		if (multi_flag == 1 && multi_OK == 1)
		{
			LT_Print_zk_Font_BUFF(encode, zk_addr,
								font_layflag, LCD_XSIZE_TFT, w, h, Alignment, fcolor, bcolor, transparent, star, FullScreen_w,FullScreen_h,m_PngData,
								xs , ys, xe-xs+1, ye-ys+1,
								0, 0, strbuf, len);
		}
		else
		{

			LT_Print_zk_Font_BUFF(encode, zk_addr,
								font_layflag, LCD_XSIZE_TFT, w, h, Alignment, fcolor, bcolor, transparent, star ,FullScreen_w,FullScreen_h,m_PngData,
								xs , ys, xe-xs+1, ye-ys+1,
								0, 0, &var[p_add], p_len * 2);
		}
	}
}

void text_display_to_buff(uint8_t type, uint8_t *buff,uint16_t overlay_x, uint16_t overlay_y, uint16_t overlay_w,uint16_t overlay_h)
{
	uint16_t xs, ys, xe, ye; // Display box
	uint8_t Zk_id, w, h;	 // Font ID
	uint8_t encode;			 // encoding type
	uint8_t Alignment, transparent, star;
	uint32_t fcolor, bcolor; // The foreground color of the font, The background color of the font
	uint16_t p_add, p_len, pointer;
	uint32_t zk_addr;
	uint8_t buff1[4] = {0};
	uint8_t lay_flag;

	uint8_t multi_flag, multi_OK;
	uint16_t multi_num;
	uint16_t len = 0;
	uint8_t strbuf[1024] = {0};
	if (type == 0) // instruction information source is configuration file
	{
		p_add = (buff[5] + (buff[6] << 8)) * 2;
		p_len = buff[7] + (buff[8] << 8);
		xs = buff[9] + (buff[10] << 8);
		ys = buff[11] + (buff[12] << 8);
		xe = buff[13] + (buff[14] << 8);
		ye = buff[15] + (buff[16] << 8);
		w = buff[17];
		h = buff[18];
		Zk_id = buff[19];
		encode = buff[20];
		Alignment = buff[21];
		bcolor = buff[22] + (buff[23] << 8) + (buff[24] << 16);
		fcolor = buff[25] + (buff[26] << 8) + (buff[27] << 16);
		transparent = buff[28] & 0x01;
		star = (buff[28] & 0x02) >> 1;
		if(UI_version >= 3100)
		{
			multi_flag = buff[29];
			multi_num = buff[30] + (buff[31] << 8);
		}
		else
		{
			multi_flag = 0;
		}
	}
	else if (type == 1) // instruction information ource is variable space
	{
		pointer = (buff[2] + (buff[3] << 8)) * 2;
		p_add = (var[pointer + 1] + (var[pointer + 2] << 8)) * 2;
		p_len = var[pointer + 3] + (var[pointer + 4] << 8);
		xs = var[pointer + 5] + (var[pointer + 6] << 8);
		ys = var[pointer + 7] + (var[pointer + 8] << 8);
		xe = var[pointer + 9] + (var[pointer + 10] << 8);
		ye = var[pointer + 11] + (var[pointer + 12] << 8);
		w = var[pointer + 13];
		h = var[pointer + 14];
		Zk_id = var[pointer + 15];
		encode = var[pointer + 16];
		Alignment = var[pointer + 17];
		bcolor = var[pointer + 18] + (var[pointer + 19] << 8) + (var[pointer + 20] << 16);
		fcolor = var[pointer + 21] + (var[pointer + 22] << 8) + (var[pointer + 23] << 16);
		transparent = var[pointer + 24] & 0x01 ;
		star = (var[pointer + 24] & 0x02) >> 1;
	}


	if (Pop_up_flag == 1) // Popup offset
	{
		xs += Pop_up_x;
		ys += Pop_up_y;
		xe += Pop_up_x;
		ye += Pop_up_y;
	}


	if (multi_flag == 1)
	{
		LT_ReadFlash_UI(buff1, addr_index[24] + multi_language *  addr_index[25] * 4 + (multi_num - 1) * 4, 4);
		zk_addr = buff1[0] + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);

		if (zk_addr != 0xFFFFFFFF)
		{
			multi_OK = 1;
			LT_ReadFlash_UI(buff1, zk_addr, 6);
			fcolor = buff1[0] + (buff1[1] << 8) + (buff1[2] << 16);
			Zk_id = buff1[3];
			len	= buff1[4] + (buff1[5] << 8);
			LT_ReadFlash_UI(strbuf, zk_addr + 6, len);
		}
		else
			multi_OK = 0;
	}
	else
		multi_OK = 1;

	if (multi_OK == 1)
	{
		LT_ReadFlash_UI(buff1, addr_index[2] + Zk_id * 8, 4);
		zk_addr = buff1[0] + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);
	}
	if (multi_flag == 1 && multi_OK == 1)
	{
		LT_ReadFlash_UI(buff1, zk_addr , 4);
		w = buff1[2];
		h = buff1[3];

		if (buff1[0] == 0x47 && buff1[1] == 0x42)	//GB2312
			encode = 0;
		else if (buff1[0] == 0x47 && buff1[1] == 0x4B)	//GBK
			encode = 1;
		else if (buff1[0] == 0x42 && buff1[1] == 0x47)	//BIG5
			encode = 2;
		else if (buff1[0] == 0x41 && buff1[1] == 0x53)	//ASCII
			encode = 4;
		else if (buff1[0] == 0x75 && buff1[1] == 0x6e)	//UINICODE
			encode = 6;
		else if (buff1[0] == 0x53 && buff1[1] == 0x53)	//str
			encode = 9;
	}

	if (pic_update == 0)
	{
		if (multi_flag == 1 && multi_OK == 1)
		{
			LT_Print_zk_Font_BUFF(encode, zk_addr,
										 font_layflag, LCD_XSIZE_TFT, w, h, Alignment, fcolor, bcolor, transparent, star, overlay_w,overlay_h,m_PngData,
										 xs - overlay_x , ys - overlay_y ,xe-xs+1,ye-ys+1,
										 0, 0, strbuf, len);
		}
		else
		{
			LT_Print_zk_Font_BUFF(encode, zk_addr,
							 font_layflag, LCD_XSIZE_TFT, w, h, Alignment, fcolor, bcolor, transparent, star, overlay_w,overlay_h,m_PngData,
							 xs - overlay_x , ys - overlay_y ,xe-xs+1,ye-ys+1,
							 0, 0, &var[p_add], p_len * 2);
		}
	}
	else
	{

		if(download_flag == 3)
		{
			if (Screen_DIR == 0 || Screen_DIR == 2)
			{
				xs += LCD_XSIZE_TFT * 2;
				xe += LCD_XSIZE_TFT * 2;
			}
			else if (Screen_DIR == 1 || Screen_DIR == 3)
			{
				ys += LCD_YSIZE_TFT * 2;
				ye += LCD_YSIZE_TFT * 2;
			}
		}

		if (multi_flag == 1 && multi_OK == 1)
		{
			LT_Print_zk_Font_BUFF(encode, zk_addr,
								 font_layflag, LCD_XSIZE_TFT, w, h, Alignment, fcolor, bcolor, transparent, star, FullScreen_w,FullScreen_h,m_PngData,
								 xs , ys, xe-xs+1, ye-ys+1,
								 0, 0, strbuf, len);
		}
		else
		{
			LT_Print_zk_Font_BUFF(encode, zk_addr,
								 font_layflag, LCD_XSIZE_TFT, w, h, Alignment, fcolor, bcolor, transparent, star, FullScreen_w,FullScreen_h,m_PngData,
								 xs , ys, xe-xs+1, ye-ys+1,
								 0, 0, &var[p_add], p_len * 2);
		}
	}
}
